// void, Obj This,Obj bld

Unit this;
point pt;   
Building building;
bool error;
bool bEnterCatapult;

Sleep(rand(100)+30);
this = This.AsUnit();

if(!.IsValid){ Sleep(rand(100)+50); return; }
if (.IsValid) {// leave parry mode at new command
	.SetParryMode(false);
	
	if(.hero.IsValid){
		if(.hero.HasItem("irage_battle")){
			if(.hero.command != "retreat_bld_notai"){
				Sleep(rand(200)+50);
				.AddCommand(true, "engage");
				return;
			}
		} else if(.hero.HasItem("irage_battle_stand")){
			if(.hero.command != "retreat_bld_notai"){
				Sleep(rand(200)+50);
				.AddCommand(true, "stand_position");
				return;
			}
		}
	}
	
	if(.item_count > 0){
		Item item;
		
		item = .FindItem("Terrain bonus I");
		if(item.IsValid)
			.RemoveItemsOfType("Terrain bonus I");
		else {
			item = .FindItem("Terrain bonus II");
			if(item.IsValid)
				.RemoveItemsOfType("Terrain bonus II");
			else {
				item = .FindItem("Terrain bonus III");
				if(item.IsValid)
					.RemoveItemsOfType("Terrain bonus III");
				else {
					item = .FindItem("Terrain bonus IV");
					if(item.IsValid)
						.RemoveItemsOfType("Terrain bonus IV");
				}
			}
		}
	}
}

building = bld.AsBuilding();

if(!building.IsValid())
	{ Sleep(20); return; }

if(.IsEnemy(building) && !building.AsTeleport.IsValid) 
	{ Sleep(20); return; }

if(!building.settlement.IsValid )
	{ Sleep(20); return; }

if(building.settlement.max_units==0)
	{ Sleep(20); return; }

if(!.AsHero.IsValid && building.IsHeirOf("BaseRuins")) 
	{ Sleep(20); return; }

bEnterCatapult = building.IsHeirOf("Catapult");

if(!bEnterCatapult) 
	.SetEntering(true);
                 
if(!building.settlement.Units().Contains(this)){

    pt = building.GetEnterPoint(this);
    //.pr("unit::enter: going");
	if( bEnterCatapult ){
	
        if(.IsMilitary)
			while(!.Goto(pt, 0, 1000, true, 5000));
	
	} else {
	    	
		while(!.GotoEnter(pt, 0, 1000, true, 5000)){
			if(.hero.IsValid){
				if(.IsMilitary)
					if(.speed_factor < 100)                                  
						.SetSpeedFactor(100);
				if(.hero.command == "retreat_bld_notai"){
					if(.stamina > 0) .SetStamina(0);
					CreateFeedback("StaminaLoss", this);
					if(.health > 200) .Damage(10);
					
					continue;
				}
				if(.hero.InHolder) continue;
			}
		}    
	}

    //.pr("unit::enter: entering");
	{	
		bool bStop=false;
		
		if(building.IsDead() && !bEnterCatapult){
		
			bStop = true;
			
		} else if( building.IsDead() && bEnterCatapult ){
		
			.Goto(pt, 200, 1000, true, 5000);
			Sleep(rand(100)+50);
			return;
			
		} else if((this.IsEnemy(building) && 
					!building.AsTeleport().IsValid()) ||
					building.settlement.max_units==0){
					
			bStop = true;
			
		}
		if(bStop){
			.Stop(10000); 
			.SetEntering(false);
			Sleep(rand(100)+50);
			return;
		}
	}
	
	building.settlement.AddUnit(this);
	
	if(.IsValid)
		if(.IsMilitary)
			if(.speed_factor < 100)                                  
				.SetSpeedFactor(100);
}

if(!bEnterCatapult) 
	.SetEntering(false);
